var WxParse = require('../../wxParse/wxParse.js');
var app = getApp()
var id;
Page({
  data:{
    // input默认是1  
    num: 1,
    // 使用data数据对象设置样式名  
    minusStatus: 'disabled',
    showView: false,
    showView2: false,
    fix_bot: true,
    toView: 'inToView01',
    current: 0,
    ndicatorDots: true, 
    interval: 2000, 
    duration: 500,
    cakes:[{
       name:"梦幻紫色奶油蛋糕双花奶油纸杯蛋糕双花奶油纸杯蛋糕纸杯",
       price:"150.00",
       desc:"西式高档单层甜品",
       dist:"免费",
    }], 
    listclass:[{
        choose:"蛋糕选择",
        ishaveChlid: true,
        children: [{
          id:'1',
          model:"奶油蛋糕",
        },{
          id:'2',
          model: "植物蛋糕",
          }, {
            id: '3',
            model: "植物蛋糕",
        }, {
          id: '4',
          model: "植物蛋糕",
          }, {
            id: '5',
            model: "植物蛋糕",
          }]
    }, {
      choose: "尺寸选择",
      ishaveChlid: true,
      children: [{
        id: '1',
        model: "奶油蛋糕",
      }, {
        id: '2',
        model: "植物蛋糕",
      }]
      }, {
        choose: "尺寸选择",
        ishaveChlid: true,
        children: [{
          id: '1',
          model: "奶油蛋糕",
        }, {
          id: '2',
          model: "植物蛋糕",
        }]
      }], 

    

  swiper:{
      indicatorDots: false,
      autoplay: false,
      interval: 5000,
      duration: 0,
      noSwiping: true
      },
  clickId:"0_0",

  },

  // date结束
  fav: function (e) {
    var goods_id = e.currentTarget.dataset.id;
    var tag = e.currentTarget.dataset.name;
    var uid = app.whereId.uid;
    var that = this;
    
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/index/shoucang',
      data: {
        goodsid: goods_id,
        uid:uid,
        tag: tag,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        console.log(res);
        if (res.data.msg == 'T'){
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 800
          })
        }else{
          wx.showToast({
            title: '取消收藏',
            icon: 'success',
            duration: 800
          })
        }
        that.setData({
          favorites: res.data.favourite,
        })
      }
    })
  },
  tabClick: function (res) {
    var that = this;
    // console.log(res.currentTarget.dataset.model);
    let p = res.currentTarget.dataset.id.split("_")[0];
    let c = res.currentTarget.dataset.id.split("_")[1];
    let list = this.data.listclass;
      for(let j = 0;j<list[p].children.length;j++){
        // console.log(j)
        list[p].children[j].color = "false";
        list[p].children[c].color= "true";
      }
      var z1 = "";
      var zid = "";
      var price = res.currentTarget.dataset.price;
      //  循环遍历外层，获取每一个外层下标值
      for (let i = 0; i < list.length; i++) {
        //console.log(list[i].children);  //打印出所有子元素
        // 获取内层循环的数值，判断color为true
        for (let j = 0; j < list[i].children.length; j++) {
          if (list[i].children[j].color == "true") {
            //console.log("就是这个" + list[i].children[j].id);
            z1 += list[i].children[j].model + " ";  //规格串联
            zid += list[i].children[j].id + "-";
          }
        }
      }
      wx.request({
        url: 'https://www.cakeyouxuan.com/small/index/float_price',
        data: {
          price: price,
          guigeid: zid,
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"  //post
        },
        success: function (res) {
          that.setData({
              prices:res.data,
            })
        }
      })
    this.setData({
      listclass: list,
      clickId: res.currentTarget.dataset.id,
      guige: z1,
    })
  },

  picClick:function(res) {
    console.log(res.currentTarget.dataset.id.split("_"));
    let p = res.currentTarget.dataset.id.split("_")[0];
    let c = res.currentTarget.dataset.id.split("_")[1];
    let pic_list = this.data.somephoto;

    for (let j = 0; j < pic_list[p].children.length; j++) {
      // console.log(j)
      pic_list[p].children[j].color1 = "false";
      pic_list[p].children[c].color1 = "true";
    }
    // var aa = res.currentTarget.dataset
    this.setData({
      somephoto: pic_list,
      clickId: res.currentTarget.dataset.id,
    })
  },


  guige: function (e) {
    console.log(e);
  },
  onPullDownRefresh: function () {
  },


  onLoad: function (options) {
    console.log(options); console.log("-----");
    let li = this.data.listclass;
    var color="";
    for(let i = 0;i<li.length;i++){
      for(let j = 0;j<li[i].children.length;j++){
        li[i].children[j].color="false";
        li[i].children[0].color = "true";
      }
    }

    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    var id = options.id;
    var uid = app.whereId.uid;

    that.setData({
      listclass : li,
      mername: options.id//options为页面路由过程中传递的参数
    });

    wx.request({
      url: 'https://www.cakeyouxuan.com/small/index/goods_info',
      data: {
        goodsid: id,
        uid: uid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        console.log(res);
        // var article = res.data.cakese.introduce;
        var article ="<p style='color:red'>adjif </p>"
        WxParse.wxParse('article', 'html', article, that, 5);
        that.setData({
          cakes: res.data.cakes,
          picture: res.data.picture,
          information: res.data.information,
          banner_list: res.data.banner_list,
          listclass: res.data.listclass,
          cakese: res.data.cakese,
          comment: res.data.comment,
          comment_num: res.data.comment_num,
          guige:'请选择规格',
          favorites: res.data.favorites,
          car_num: res.data.car_num,
          goods_id: id,
          prices: res.data.cakes[0].price,
        });
      }
    })
    showView: (options.showView == "true" ? true : false);
    showView2: (options.showView2 == "true" ? true : false)
    
  },

  onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },

  onChangeShowState1: function () {
    var that = this;
    that.setData({
      showView2: (!that.data.showView2)
    })
  },
  onChangeShowState2: function () {
    var that = this;
    that.setData({
      showView2: (!that.data.showView2),
      fix_bot: true
    })
  },
  jrgwc:function(){
    var that = this;
    that.setData({
      showView2: (!that.data.showView2),
      fix_bot: (!that.data.fix_bot)
    })
  },
  ljgm:function(){
    var that = this;
    that.setData({
      showView2: (!that.data.showView2),
      fix_bot: (!that.data.fix_bot)
    })
  },


  scrollToViewFn: function (e) {
    // var _id = e.target.dataset.id;
    this.setData({
      toView: 'inToView' + 2,
      current: e.target.dataset.index
    }),
      console.log(this.data.toView)
  },
  scrollToViewFn1: function (e) {
    // var _id = e.target.dataset.id;
    this.setData({
      toView: 'inToView' + 1,
      current: e.target.dataset.index
    })
    console.log(this.data.toView)
  },
  scrollToViewFn2: function (e) {
    // var _id = e.target.dataset.id;
    this.setData({
      toView: 'inToView' + 3,
      current: e.target.dataset.index
    })
    console.log(this.data.toView)
  },

  intcart: function (e) {
    wx.navigateTo({
      url: "/pages/yiguo/cart/cart"
    })
  },
  get_shop: function(e){
      wx.switchTab({
        url: '/pages/yiguo/store/store',
      })
  },
  check_all:function(e){
    wx.navigateTo({
      url: "/pages/yiguo/pinglun/pinglun"
    })
  },
  get_mine: function (e) {
    wx.switchTab({
      url: '/pages/yiguo/mine/mine',
    })
  },
  switchSlider:function(e){
    this.setData({
      current:e.target.dataset.index
    })
  },
  selected: function (e) {
    this.setData({
      current: e.target.dataset.index1
    })
  },
  changeSlider:function(e){
    this.setData({
      current: e.detail.current
    })
  },
  chatting: function (e) {
    wx.switchTab({
      url: '/pages/yiguo/custom-message/custom-message',
    })
  },

  piclick: function(res){
    console.log(res);
    this.setData({
      // tabId: res.currentTarget.dataset.size,
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    let li = this.data.listclass;
    var color = "";
    for (let i = 0; i < li.length; i++) {
      for (let j = 0; j < li[i].children.length; j++) {
        li[i].children[j].color = "false";
        li[i].children[0].color = "true";
        // console.log(11);
      }
    }
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  //做滑动事件
  changeIndicatorDots1: function (e) {
    console.log(e);
    this.setData({
      //indicatorDots: !this.data.indicatorDots,
    })
    // console.log("indi::::" + e.detail.current);
    var number = e.detail.current;
    console.log(number);
  },

  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减  
    if (num > 1) {
      num--;
    }
    wx.setStorageSync('nums', num);
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1  
    num++;

    wx.setStorageSync('nums',num);
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回  
    this.setData({
      num: num
    });
  },

  //加入购物车  
  buy: function (e) { //加入购物车
    var that = this;
    if (wx.getStorageSync('nums')){
      var nums = wx.getStorageSync('nums');
    }else{
      var nums = 1;
    }

    // list获取服务器数组
    let list = this.data.listclass;
    var z1 = "";
    //  循环遍历外层，获取每一个外层下标值
    for (let i = 0; i < list.length; i++) {
      //console.log(list[i].children);  //打印出所有子元素
      // 获取内层循环的数值，判断color为true

      for (let j = 0; j < list[i].children.length; j++) {
        if (list[i].children[j].color == "true") {
          //console.log("就是这个" + list[i].children[j].id);
          z1 += list[i].children[j].id + "-";  //规格串联
        }
      }
    }
    var uid = app.whereId.uid;
    var shopid = app.whereId.shopid;
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/cart/car_add',
      data: {
        guige: z1,
        uid: uid,
        goodid: e.currentTarget.dataset.id,
        num: nums,
        tag: 1,
        shopid: shopid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        console.log(res);
        wx.removeStorageSync('nums');
        wx.showToast({
          title: '成功加入购物车',
          icon: 'success',
          duration: 800
        })
        that.setData({
          car_num: res.data.car_num,
          showView2: (!that.data.showView2),
          fix_bot: true
        })
      },
    })
  },

  //立即购买
  stap: function (e) {  //立即购买
    var that = this;
    if (wx.getStorageSync('nums')) {
      var nums = wx.getStorageSync('nums');
    } else {
      var nums = 1;
    }

    // list获取服务器数组
    let list = this.data.listclass;
    var z1 = "";
    //  循环遍历外层，获取每一个外层下标值
    for (let i = 0; i < list.length; i++) {
      //console.log(list[i].children);  //打印出所有子元素
      // 获取内层循环的数值，判断color为true

      for (let j = 0; j < list[i].children.length; j++) {
        if (list[i].children[j].color == "true") {
          //console.log("就是这个" + list[i].children[j].id);
          z1 += list[i].children[j].id + "-";  //规格串联
        }
      }
    }
    var shopid = app.whereId.shopid;
    var uid = app.whereId.uid;
    if (z1 == ""){
      wx.showToast({
        title: '请先选择规格',
        icon: 'success',
        duration: 800
      })
    }else{
      wx.request({
        url: 'https://www.cakeyouxuan.com/small/cart/car_add',
        data: {
          guige: z1,
          uid: uid,
          num: nums,
          goodid: e.currentTarget.dataset.id,
          tag: 0,
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"  //post
        },
        success: function (res) {
          console.log(res);
          wx.navigateTo({
            url: "/pages/yiguo/detail/detail?uid=" + uid + "&shopid=" + shopid + "&cid=" + res.data.car,
          })
        },
      })
    }
  },

  //查看全部评价
  look_comment: function (e) {
    console.log(e);
    var goodid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/yiguo/pinglun/pinglun?ids=" + goodid,
    })
  },

  //购物车图标按钮
  gouwuche:function(){
    wx.switchTab({
      url: "/pages/yiguo/cart/cart",
    })
  },
})
